package com.example.rg1gameactivityapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class StateActivity extends AppCompatActivity {

    private TextView textViewName;
    private TextView textViewPWD;

    private ProgressBar progressBar1;
    private ProgressBar progressBar2;
    private ProgressBar progressBar3;
    private Button buttonBuy;
    private Button buttonClear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state);
        textViewName=findViewById(R.id.textViewName);
        textViewPWD=findViewById(R.id.textViewPWD);
        final Intent intent=getIntent();
        textViewName.setText(intent.getStringExtra("userName"));
        textViewPWD.setText(intent.getStringExtra("userPWD"));
        progressBar1=findViewById(R.id.progressBar1);
        progressBar2=findViewById(R.id.progressBar2);
        progressBar3=findViewById(R.id.progressBar3);
        buttonBuy=findViewById(R.id.buttonBuy);
        buttonClear=findViewById(R.id.buttonClear);
        readWeapon();
        buttonBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent(StateActivity.this,ShopActivity.class);
                startActivityForResult(intent1,1);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Weapon weapon=(Weapon) data.getSerializableExtra("weapon");
        progressBar1.setProgress(progressBar1.getProgress()+weapon.getAttack());
        progressBar2.setProgress(progressBar2.getProgress()+weapon.getDefence());
        progressBar3.setProgress(progressBar3.getProgress()+weapon.getSpeed());
        saveWeapon();
    }

    private void saveWeapon()
    {
        Weapon currentWeapon=new Weapon("weapon",progressBar1.getProgress(),progressBar2.getProgress(),progressBar3.getProgress());
        Gson gson=new Gson();
        String weaponJSON=gson.toJson(currentWeapon);
        Toast.makeText(this,weaponJSON,Toast.LENGTH_SHORT).show();
        FileOutputStream out= null;
        try {
            out = openFileOutput("weapon.txt",MODE_PRIVATE);
            out.write(weaponJSON.getBytes());
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void readWeapon()
    {
        FileInputStream in= null;
        try {
            in = openFileInput("weapon.txt");
            byte[] buffer=new byte[in.available()];
            in.read(buffer);
            String weaponJSON=new String(buffer);
            Toast.makeText(this,weaponJSON,Toast.LENGTH_SHORT).show();
            Gson gson=new Gson();
            Weapon currentWeapon=gson.fromJson(weaponJSON,Weapon.class);
            progressBar1.setProgress(currentWeapon.getAttack());
            progressBar2.setProgress(currentWeapon.getDefence());
            progressBar3.setProgress(currentWeapon.getSpeed());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
